/**
 * 
 */
package com.rio.dandan.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.SimpleByteSource;

/**
 * 
 * PasswordUtils.java
 * 
 * @author 宋陆
 * @version 1.0 2014年5月20日
 */
public class PasswordUtils {
    public static Password encryptPwd(String pwd) {
        RandomNumberGenerator rng = new SecureRandomNumberGenerator();
        ByteSource salt = rng.nextBytes();
        ByteSource pSalt = new SimpleByteSource(salt.toBase64());
        Sha256Hash hash = new Sha256Hash(pwd, pSalt);

        return new Password(hash.toBase64(), salt.toBase64());
    }

    /**
     * 
     * PasswordUtil.java
     * 
     * @author 宋陆
     * @version 1.0 2014年5月20日
     */
    public static class Password {

        /** The pwd. */
        private String pwd;

        /** The pwd salt. */
        private String salt;

        /**
         * Instantiates a new password.
         * 
         * @param pwd
         *            the pwd
         * @param pwdSalt
         *            the pwd salt
         */
        public Password(String pwd, String salt) {
            this.pwd = pwd;
            this.salt = salt;
        }

        /**
         * Gets the pwd.
         * 
         * @return the pwd
         */
        public String getPwd() {
            return pwd;
        }

        /**
         * Sets the pwd.
         * 
         * @param pwd
         *            the new pwd
         */
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }

        /**
         * @return the salt
         */
        public String getSalt() {
            return salt;
        }

        /**
         * @param salt
         *            the salt to set
         */
        public void setSalt(String salt) {
            this.salt = salt;
        }

    }
}
